package view;

import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.Window;

import javafx.scene.Parent;

import javax.swing.JFrame;
import javax.swing.JMenuBar;
import javax.swing.JMenu;
import javax.swing.JMenuItem;
import javax.swing.JScrollPane;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.JTextField;
import java.awt.Component;
import javax.swing.JTree;
import javax.swing.JPopupMenu;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.beans.Transient;
import java.sql.DatabaseMetaData;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Enumeration;
import java.util.Observable;
import java.util.Observer;

import javax.swing.JRadioButtonMenuItem;
import javax.swing.tree.DefaultTreeModel;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.MutableTreeNode;
import javax.swing.tree.TreeModel;
import javax.swing.tree.TreeNode;
import javax.swing.tree.TreeSelectionModel;

public class Gui implements Observer {

	private JFrame frame;
	private JTextField textField;
	private static Gui instance = null;
	private JTree tree;
	private DefaultTreeModel model;

	/**
	 * Launch the application.
	 */

	public static Gui getInstance() {
		if (instance == null) {
			instance = new Gui();
			instance.frame.setVisible(true);
		}
		return instance;
	}

	/**
	 * Create the application.
	 */
	public Gui() {
		frame = new JFrame();
		frame.setBounds(100, 100, 500, 500);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

		JPopupMenu popupMenu = new JPopupMenu();
		addPopup(frame, popupMenu);

		JMenuBar menuBar = new JMenuBar();
		frame.setJMenuBar(menuBar);

		JMenu mnFile = new JMenu("File");
		menuBar.add(mnFile);

		JMenuItem Load = new JMenuItem("Load");
		Load.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				OpenDialog odc = new OpenDialog();
				odc.addObserver(getInstance());
				odc.oeffnen();
			}
		});

		mnFile.add(Load);

			DefaultMutableTreeNode root = new DefaultMutableTreeNode(
					"Database's");
			model = new DefaultTreeModel(root);
			tree = new JTree(model);
			tree.setModel(model);
			tree.updateUI();
			tree.setVisible(true);

		
		tree.setShowsRootHandles(true);
		tree.setBounds(0, 0, 100, 440);
		frame.getContentPane().add(tree);

		JMenu Edit = new JMenu("Edit");
		menuBar.add(Edit);

		JMenu Help = new JMenu("Help");
		menuBar.add(Help);

		frame.getContentPane().setLayout(null);

		textField = new JTextField();
		textField.setBounds(100, 0, 384, 20);
		frame.getContentPane().add(textField);
		textField.setColumns(10);

		JRadioButtonMenuItem rdbtnmntmNewRadioItem = new JRadioButtonMenuItem(
				"New radio item");
		rdbtnmntmNewRadioItem.setBounds(100, -2, 125, 22);
		frame.getContentPane().add(rdbtnmntmNewRadioItem);
	}

	private static void addPopup(Component component, final JPopupMenu popup) {
		component.addMouseListener(new MouseAdapter() {
			public void mousePressed(MouseEvent e) {
				if (e.isPopupTrigger()) {
					showMenu(e);
				}
			}

			public void mouseReleased(MouseEvent e) {
				if (e.isPopupTrigger()) {
					showMenu(e);
				}
			}

			private void showMenu(MouseEvent e) {
				popup.show(e.getComponent(), e.getX(), e.getY());
			}
		});
	}

	public void update(Observable arg0, Object arg1) {
		final DatabaseMetaData md = (DatabaseMetaData) arg1;
		DefaultMutableTreeNode root = new DefaultMutableTreeNode("Database's");
		try {
			ResultSet rs = md.getTables(null, null, "%", null);
			while (rs.next()) {
				root.add(new DefaultMutableTreeNode(rs.getString(3)));
			}
			model = new DefaultTreeModel(root);
			tree.setModel(model);
			tree.updateUI();
			

		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}